<?php
namespace app\api\controller\member;
use app\common\controller\BaseController;
use app\common\model\setting\SettingStorePlatformModel;
use app\common\service\api\VrabbitSdkService;
use app\common\service\member\MemberAccountService;
use app\common\service\setting\RegionService;
use app\common\service\setting\SettingPriceRangeService;
use app\common\service\setting\SettingStorePlatformService;
use app\common\service\setting\SettingStoreTypeService;
use app\common\validate\member\MemberAccountValidate;
use hg\apidoc\annotation as Apidoc;
/**
 * @Apidoc\Title("接单账号")
 * @Apidoc\Group("member")
 * @Apidoc\Sort("250")
 */
class MemberAccount extends BaseController
{
    /**
    * @Apidoc\Title("接单账号列表")
    * @Apidoc\Query(ref="pagingQuery")
    * @Apidoc\Query(ref="sortQuery")
    * @Apidoc\Query(ref="searchQuery")
    * @Apidoc\Query(ref="dateQuery")
    * @Apidoc\Returned(ref="expsReturn")
    * @Apidoc\Returned(ref="pagingReturn")
    * @Apidoc\Returned("list", type="array", desc="接单账号列表", children={
    *   @Apidoc\Returned(ref="app\common\model\member\MemberAccountModel", field="id,title,content,is_disable,create_time,update_time,member_id,store_platform_id,nickname,accname,level,sex,age,integral,consignee,phone,region_id,address,status,auth_msg,sort,remark")
    * })
    */
    public function list()
    {
        $where = $this->buildWhere([
            'member_id',
            'store_platform_id',
            'setting_store_type_ids',
            'is_disable',
            'status',
            'level'
        ]);
        $where = $this->where(where_delete($where));
        $where[] = ['member_id','=',member_id(true)];
        $where[] = ['status','=',1];
        $data = MemberAccountService::list($where, 0, 0, ['store_platform_id'=>'asc','serial_num'=>'asc'],'id,store_platform_id,accname,is_order_taking');
        return success($data);
    }
    /**
     * @Apidoc\Title("查询参数")
     * @Apidoc\Query(ref="app\common\model\member\MemberAccountModel", field="id")
     * @Apidoc\Returned(ref="app\common\model\member\MemberAccountModel")
     */
    public function getParams()
    {
        $where = $this->where(where_delete());
        $data = MemberAccountService::getParams();
        return success($data);
    }
    /**
    * @Apidoc\Title("接单账号信息")
    * @Apidoc\Query(ref="app\common\model\member\MemberAccountModel", field="id")
    * @Apidoc\Returned(ref="app\common\model\member\MemberAccountModel")
    */
    public function info()
    {
        $param = $this->params(['id/d' => '']);
        validate(MemberAccountValidate::class)->scene('info')->check($param);
        $data = MemberAccountService::info($param['id']);
        return success($data);
    }
    /**
    * @Apidoc\Title("接单账号添加")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="title,content,is_disable,member_id,store_platform_id,nickname,accname,level,sex,age,integral,consignee,phone,region_id,address,status,auth_msg,sort,remark")
    */
    public function add()
    {
        $param = $this->params(MemberAccountService::$edit_field);
        validate(MemberAccountValidate::class)->scene('add')->check($param);
        $data = MemberAccountService::add($param);
        return success($data);
    }
    /**
    * @Apidoc\Title("接单账号修改")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="id,title,content,is_disable,member_id,store_platform_id,nickname,accname,level,sex,age,integral,consignee,phone,region_id,address,status,auth_msg,sort,remark")
    */
    public function edit()
    {
        $param = $this->params(MemberAccountService::$edit_field);
        validate(MemberAccountValidate::class)->scene('edit')->check($param);
        $data = MemberAccountService::edit($param['id'], $param);
        return success($data);
    }
    /**
    * @Apidoc\Title("接单账号删除")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="idsParam")
    */
    public function dele()
    {
        $param = $this->params(['ids/a' => []]);
        validate(MemberAccountValidate::class)->scene('dele')->check($param);
        $data = MemberAccountService::dele($param['ids']);
        return success($data);
    }
    /**
     * @Apidoc\Title("接单账号禁用")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="is_disable")
     */
    public function disable()
    {
        $param = $this->params(['ids/a' => [], 'is_disable/d' => 0]);
        validate(MemberAccountValidate::class)->scene('disable')->check($param);
        $data = MemberAccountService::edit($param['ids'], $param);
        return success($data);
    }

    
    /**
     * @Apidoc\Title("接单账号审核")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="status,auth_msg")
     */
    public function is_auth()
    {
        $param = $this->params(['ids/a' => [], 'status/d' => 0, 'auth_msg/s' => null]);
        validate(MemberAccountValidate::class)->scene('auth')->check($param);
        $param['auth_time'] = date('Y-m-d H:i:s');
        $param['auth_uid']  = user_id();
        $data = MemberAccountService::edit($param['ids'], $param);
        return success($data);
    }

    /**
     * @Apidoc\Title("查询平台列表")
     * @Apidoc\Query(ref="app\common\model\member\MemberAccountModel", field="id")
     * @Apidoc\Returned(ref="app\common\model\member\MemberAccountModel")
     */
    public function getPlatforms()
    {
        $where = $this->where(where_delete());
        $where[] = ['is_disable','=',0];
        $where[] = ['code','<>',SettingStorePlatformModel::CODE_TM];
        $data = SettingStorePlatformService::list($where,0,0, $this->order(),'id as value,title as label,image_id,account_number,code');
        $list = [];
        $member_id = member_id(true);
        foreach ($data as $key=>$val){
            //查询账号
            $ac_where = array(
                ['is_disable','=',0],
                ['is_delete','=',0],
                ['member_id','=',$member_id],
                ['store_platform_id','=',$val['value']],
            );
            $account_list = MemberAccountService::list($ac_where, 0, 0, ['serial_num' => 'asc'],'id,accname,status,serial_num');
            for($i=0;$i<$val['account_number'];$i++){
                $val['serial_num'] = $i;
                $val['account'] = null;
                foreach ($account_list as $k=>$v){
                    if($val['serial_num']>=0 && $val['serial_num']==$v['serial_num']){
                        $val['account'] =$v;
                        break;
                    }else if(!$v['serial_num'] && $val['serial_num'] == $k){
                        $val['account'] =$v;
                        break;
                    }
                }
                array_push($list,$val);
            }
        }
        return success($list);
    }


    /**
     * @Apidoc\Title("查询新增、编辑账号页面参数")
     * @Apidoc\Query(ref="app\common\model\member\MemberAccountModel", field="id")
     * @Apidoc\Returned(ref="app\common\model\member\MemberAccountModel")
     */
    public function getEditParams()
    {
        //店铺平台
        $param = $this->params([
            'store_platform_id/d' =>null,
        ]);
        if($param['store_platform_id']){
            $platform = SettingStorePlatformService::info($param['store_platform_id'],true,'id,title,code,evaluate_image_id,last_month_image_id,month_image_id,personal_center_image_id');
            $data['platform'] =$platform;
        }else{
            exception('请选择平台');
        }
        //年龄段
        $data['age_list'] = SettingPriceRangeService::getAgeList();
        //常购类目
        $type_where = $this->where(where_delete());
        $type_where[] = ['is_disable','=',0];
        $data['setting_store_types'] = SettingStoreTypeService::list($type_where,0,0, $this->order(),'id,id as value,title as label,false as checked');
        //信用等级
        $params_data = MemberAccountService::getParams();
        $data['level_list'] =$params_data['level_list'];
        //地区
        $data['region_list'] = RegionService::list('tree', [where_delete()], [], 'region_id,region_pid,region_name');
        return success($data);
    }
    /**
     * @Apidoc\Title("接单账号保存")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="title,content,is_disable,member_id,store_platform_id,nickname,accname,level,sex,age,integral,consignee,phone,region_id,address,status,auth_msg,sort,remark")
     */
    public function save()
    {
        $param = $this->params([
            'id/d'=>null,
            'store_platform_id/d' =>null,
            'serial_num/d'=>0,//序号
            'accname/s'=>null,//账号
            'sex/d'=>null,//性别
            'age_index/d'=>null,//年龄段
            'setting_store_type_ids/a'=>[],//常购类目
            'level/d'=>null,//信用等级
            'integral/d'=>null,//淘气值
            'consignee/s'=>null,//收货人
            'phone/s'=>null,//收货电话
            'region_id/d'=>null,//收货地址
            'address/s'=>null,//收货详细地址
            'evaluate_image_id/d'=>null,//订单评价截图
            'last_month_image_id/d'=>null,//上月订单截图
            'month_image_id/d'=>null,//上上月订单截图
            'personal_center_image_id/d'=>null,//上上月订单截图
            'taoling/d'=>null,//淘龄
            'is_member/d'=>0,//是否是会员
        ]);
        $param['member_id'] =member_id(true);
        validate(MemberAccountValidate::class)->scene('save')->check($param);
        $data = MemberAccountService::save($param);
        return success($data);
    }


    /**
     * @Apidoc\Title("接单账号开启与关闭")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\member\MemberAccountModel", field="title,content,is_disable,member_id,store_platform_id,nickname,accname,level,sex,age,integral,consignee,phone,region_id,address,status,auth_msg,sort,remark")
     */
    public function setOrderTaking()
    {
        $param = $this->params([
            'account_list/a'=>null,
        ]);
        $param['member_id'] =member_id(true);
        validate(MemberAccountValidate::class)->scene('setOrderTaking')->check($param);
        $data = MemberAccountService::setOrderTaking($param['member_id'],$param);
        return success($data);
    }
}
